<?php

namespace App\Http\Controllers\Home;

use App\Model\BranchTest;
use App\Model\User;
use App\Model\UserBranchTest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class PermissionController extends Controller
{
    // 列表页
    public function index(Request $request)
    {
        $user = DB::table('user')
            ->join('user_branchtest', 'user.id', '=', 'user_branchtest.uid')
            ->join('branchtest', 'branchtest.id', '=', 'user_branchtest.role_id')
            ->select('user.*','user.username as name', 'branchtest.username')
            ->get();
        return view("home.permission.list",compact("user","request"));
    }




    //用户添加页面
    public function create()
    {
        return view("home.permission.add");
    }

    public function show($id){

    }

    //添加操作
    public function store(Request $request)
    {
        $user = $request->post();

        $user['username'] = htmlspecialchars($user['username']);
        $user['password'] = Hash::make($user['password']);

        $res = User::create($user);
        if($res){
            echo json_encode([
                "code"=>1,
                "msg"=>"success"
            ]);
        }else{
            echo json_encode([
                "code"=>0,
                "msg"=>"error"
            ]);
        }
    }

    //返回修改页面
    public function edit($id)
    {
        $user = User::find($id);
        $role = BranchTest::orderBy("id","desc")->select('id','username')->get();
        return view("home.permission.edit",compact('user','role'));
    }

    //执行修改操作
    public function update(Request $request, $id)
    {

        $user = $request -> all();

        if(isset($user['s'])){
            unset($user['s']);
            unset($user['username']);
            unset($user['id']);
        }

        $res = UserBranchTest::where("uid",$id)->update($user);

        if($res){
            echo json_encode([
                "code"=>1,
                "msg"=>"success"
            ]);
        }else{
            echo json_encode([
                "code"=>0,
                "msg"=>"error"
            ]);
        }
    }

    //删除操作
    public function destroy(Request $request,$id)
    {
        $user = User::find($id);
        $res = $user->delete();
        if($res){
            $data = array(
                "status"=>0,
                "message"=>"删除成功"
            );
        }else{
            $data = array(
                "status"=>1,
                "message"=>"删除失败"
            );
        }
        return $data;
    }

    //批量删除
    public function delAll(Request $request){
        $res = User::destroy($request->input('ids'));
        if($res){
            $data = array(
                "status"=>0,
                "message"=>"删除成功"
            );
        }else{
            $data = array(
                "status"=>1,
                "message"=>"删除失败"
            );
        }
        return $data;
    }
}
